package com.jiyi.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.jiyi.inteface.SygnPopInterface;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.ToastUtil;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class DataInputSygnPopView{

	private SygnPopInterface mSygnPopInterface;
	private Context context;
	private PopupWindow mPopupWindow;

	EditText etFirst,etEnd,etFunc;


	public DataInputSygnPopView(Context context,SygnPopInterface mSygnPopInterface) {
		this.context = context;
		this.mSygnPopInterface = mSygnPopInterface;
		init();
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(初始化一个新的PopWindow)
	 * @param:
	 * @return: void
	 * @return: 2017-5-5
	 * @throws
	 */
	private void init() {
		// 得到内容布局
		View view = LayoutInflater.from(context).inflate(R.layout.pop_datainput_sygn,
				null);

		etFirst = view.findViewById(R.id.etFirst);
		etEnd = view.findViewById(R.id.etEnd);
		etFunc = view.findViewById(R.id.etFunc);

		// 取消按钮
		TextView tvCancle = (TextView) view.findViewById(R.id.tvCancle);
		tvCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mSygnPopInterface.OnCancleAddListener();
				hidePopWindow();
			}
		});

		// 确定按钮
		TextView tvDelete = (TextView) view.findViewById(R.id.tvSure);
		tvDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			    if (TextUtils.isEmpty(etFirst.getText()) || TextUtils.isEmpty(etEnd.getText())  || TextUtils.isEmpty(etFunc.getText()))
                {
                    ToastUtil.getInstances().showMsg("请先完善内容");
			        return;
                }
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append(etFirst.getText().toString()).append("层").append("  ---  ").append(etEnd.getText().toString()).append("层")
				.append("	").append("功能：").append(etFunc.getText().toString());
				mSygnPopInterface.OnSureAddListener(stringBuilder.toString());
			}

		});
		mPopupWindow = new PopupWindow(view,
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true); // 让popWindow获取焦点
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置popWindow背景颜色
		mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
		mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				hidePopWindow();
			}
		});
	}

	/**
	 * @author: HYM
	 * @Title: showPopWindow
	 * @Description: TODO(显示弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void showPopWindow() {
		mPopupWindow
				.showAtLocation(new TextView(context), Gravity.CENTER, 0, 0);
		backgroundAlpha(0.5F);
	}

	/**
	 * @author: HYM
	 * @Title: hidePopWindow
	 * @Description: TODO(关闭弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void hidePopWindow() {
		etFunc.setText("");
		etFirst.setText("");
		etEnd.setText("");
		if (mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
		backgroundAlpha(1F);

        InputMethodManager mInputMethodManager = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(etFunc.getWindowToken(), 0);
	}

	/**
	 * 设置添加屏幕的背景透明度
	 *
	 * @param bgAlpha
	 */
	public void backgroundAlpha(float bgAlpha) {
		Activity activity = (Activity) context;
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0-1.0
		activity.getWindow().setAttributes(lp);

		if (bgAlpha == 1)
		{
			activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
		else
		{
			activity.getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
	}

}
